package com.etl.controller;

import com.etl.constants.UserSession;
import com.etl.entity.EtlBusinessBatch;
import com.etl.enums.ResultEnum;
import com.etl.model.rev.EtlBusinessBatchRev;
import com.etl.service.IEtlBusinessBatchService;
import com.etl.service.IEtlTaskIncrementService;
import com.etl.service.IEtlTaskMainService;
import com.etl.util.ResponseUtil;
import com.etl.util.ResultVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * Created by Administrator on 2019/10/21.
 */
@RestController
@RequestMapping("/etlBusinessBatch")
@Api(tags = {"批次管理"}, value = "批次增删改查相关接口")
@Validated
public class EtlBusinessBatchController {

    @Resource
    private IEtlBusinessBatchService etlBatchService;

    @Resource
    private IEtlTaskMainService etlTaskMainService;

    @Resource
    private IEtlTaskIncrementService etlTaskIncrementService;


    /**
     * 【批次】查询列表
     *
     * @param pages
     * @param sizes
     * @return
     */

    @ApiOperation(value = "查询抽取系统列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pages", value = "页数", example = "1"),
            @ApiImplicitParam(paramType = "query", name = "sizes", value = "数量", example = "10")
    })
    @GetMapping(value = "/getEtlBusinessBatchList")
    public ResultVO<PageInfo<EtlBusinessBatch>> getEtlBatchList(Integer pages, Integer sizes, String name) {
        PageInfo<EtlBusinessBatch> pageInfo = etlBatchService.getEtlBatchList(pages, sizes,name);
        return ResponseUtil.success(pageInfo);
    }

    /**
     * 【批次】 添加抽取批次
     *
     * @param batchName
     * @return
     */

    @ApiOperation(value = "添加抽取批次")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", name = "sysName", value = "业务系统名称", example = "成本系统"),
            @ApiImplicitParam(paramType = "form", name = "dbId", value = "数据库id", example = "成本系统"),
            @ApiImplicitParam(paramType = "form", name = "dbName", value = "数据库名称", example = "成本系统")
    })
    @PostMapping(value = "/addEtlBusinessBatch")
    public ResultVO addBatch(@NotBlank(message = "批次名称不能为空") String batchName,
                             @NotNull(message = "数据库id不能为空") Integer sourceDbId,
                             @NotNull(message = "数据库id不能为空")  Integer targetDbId) {

        etlBatchService.addBatch(batchName,sourceDbId,targetDbId);
        return ResponseUtil.success();
    }


    /**
     * 【批次】 更新批次定时时间
     * @return
     */
    @ApiOperation(value = "更新抽取批次 - 设置定时任务")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "batch", value = "批次", example = "1"),
            @ApiImplicitParam(paramType = "query", name = "taskVal", value = "定时任务值", example = "10"),
            @ApiImplicitParam(paramType = "query", name = "taskType", value = "定时任务类型 0：固定时间，1：按时间隔，2：按分间隔，3：按秒间隔", example = "1")
    })
    @PutMapping(value = "/updateEtlBusinessBatch")
    public ResultVO updateBatch(@RequestBody EtlBusinessBatchRev etlBusinessBatchRev) {
//        if (batch == null || taskVal == null || taskType == null) {
//            return error(ResultEnum.PARAMS_ERROR);
//        }
//        EtlBusinessBatch etlBusinessBatch = new EtlBusinessBatch();
//        etlBusinessBatch.setBatch(batch).setTaskType(taskType).setTaskVal(taskVal);
        EtlBusinessBatch etlBusinessBatch = new EtlBusinessBatch();
        BeanUtils.copyProperties(etlBusinessBatchRev,etlBusinessBatch);
        etlBatchService.updateBatch(etlBusinessBatch);
        return ResponseUtil.success();
    }

    /**
     * 【批次】 启动批次
     */
    @ApiOperation(value = "启动批次")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "batch", value = "批次", example = "1"),
            @ApiImplicitParam(paramType = "query", name = "status", value = "状态", example = "1")
    })
    @PostMapping(value = "/startEtlBusinessBatch")
    public ResultVO startBatch(Integer batch, Integer status) {
        if (batch == null || status == null || status > 1 || status < 0) {
            return ResponseUtil.error(ResultEnum.PARAMS_ERROR);
        }
        EtlBusinessBatch etlBusinessBatch = new EtlBusinessBatch();
        etlBusinessBatch.setBatch(batch);
        etlBusinessBatch.setFlag(status);
        etlBatchService.startBatch(etlBusinessBatch);
        return ResponseUtil.success();
    }


    /**
     * 【批次】 删除批次
     */
    @ApiOperation(value = "删除批次")
    @ApiImplicitParam(paramType = "query", name = "batch", value = "批次", example = "1")
    @GetMapping(value = "/deleteEtlBusinessBatch")
    public ResultVO deleteEtlBusinessBatch(@NotNull(message = "批次id不能为空") Integer batch) {
        EtlBusinessBatch etlBusinessBatch = new EtlBusinessBatch();
        etlBusinessBatch.setBatch(batch);
        etlBusinessBatch.setFlag(0);
        etlBatchService.startBatch(etlBusinessBatch);
        return ResponseUtil.success();
    }



    @ApiOperation(value = "执行批次下所有任务")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "batch", value = "批次号", example = "1", required = true)
    })
    @GetMapping(value = "/executeAll")
    public ResultVO executeAll(Integer batch) {
//        etlTaskMainService.delayExecuteAll(batch);
//        etlTaskMainService.executeAll(batch, UserSession.getUser().getAccountName());
        etlTaskIncrementService.executeAll(batch, UserSession.getUser().getAccountName());
        return ResponseUtil.success();
    }


}

